<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:insert="include :: header('新增用户')"/>
    <!--	<th:block th:insert="include :: select2-css" />-->
</head>
<body>
<div class="layui-container">
    <form class="layui-form">
        <input name="deptId" type="hidden" id="treeId"/>
        <fieldset class="layui-elem-field layui-field-title">
            <legend>基本信息</legend>
        </fieldset>
        <div class="layui-row">
            <div class="layui-col-sm6 layui-col-md6">
                <div class="layui-form-item">
                    <label class="layui-form-label"><span style="color: red; ">*</span>用户名称：</label>
                    <div class="layui-input-block">
                        <input type="text" name="userName" placeholder="请输入用户名称" autocomplete="off" class="layui-input" maxlength="30" required>
                    </div>
                </div>
            </div>
            <div class="layui-col-sm6 layui-col-md6">
                <div class="layui-form-item">
                    <label class="layui-form-label"><span style="color: red; ">*</span>归属部门：</label>
                    <div class="layui-input-block">
                        <div class="input-group">
                            <input name="deptName" onclick="selectDeptTree()" id="treeName" type="text" placeholder="请选择归属部门" class="form-control"
                                   required>
                            <span class="input-group-addon"><i class="fa fa-search"></i></span>
                        </div>

                    </div>
                </div>
            </div>
        </div>

        <div class="layui-row">
            <div class="layui-col-sm6 layui-col-md6">
                <div class="layui-form-item">
                    <label class="layui-form-label"><span style="color: red; ">*</span>手机号码：</label>
                    <div class="layui-input-block">
                        <input type="text" name="phonenumber" placeholder="请输入手机号码" class="layui-input" maxlength="11" required
                               lay-verify="required|phone">
                    </div>
                </div>
            </div>
            <div class="layui-col-sm6 layui-col-md6">
                <div class="layui-form-item">
                    <label class="layui-form-label"><span style="color: red; ">*</span>邮箱：</label>
                    <div class="layui-input-block">
                        <div class="input-group">
                            <input name="email" class="layui-input" type="text" maxlength="50" placeholder="请输入邮箱" required
                                   lay-verify="required|email">
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="layui-row">
            <div class="layui-col-sm6 layui-col-md6">
                <div class="layui-form-item">
                    <label class="layui-form-label"><span style="color: red; ">*</span>登录账号：</label>
                    <div class="layui-input-block">
                        <input type="text" name="loginName" placeholder="请输入登录账号" class="layui-input" maxlength="30" required lay-verify="required">
                    </div>
                </div>
            </div>
            <div class="layui-col-sm6 layui-col-md6">
                <div class="layui-form-item">
                    <label class="layui-form-label"><span style="color: red; ">*</span>登录密码：</label>
                    <div class="layui-input-block">
                        <input type="password" name="password" placeholder="请输入登录密码" class="layui-input" maxlength="11" required
                               lay-verify="required|password">
                    </div>
                </div>
            </div>
        </div>


        <div class="layui-row">
            <div class="layui-col-sm6 layui-col-md6">
                <div class="layui-form-item">
                    <label class="layui-form-label"><span style="color: red; ">*</span>用户性别：</label>
                    <div class="layui-input-block">

                        <input type="radio" name="sex" th:each="dict : ${@dict.getType('sys_user_sex')}" th:title="${dict.dictLabel}"
                               th:value="${dict.dictValue}"/>

                        <!--
                         <select name="sex" class="form-control m-b" th:with="type=${@dict.getType('sys_user_sex')}">
                             <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                         </select>-->
                    </div>
                </div>
            </div>
            <div class="layui-col-sm6 layui-col-md6">
                <div class="layui-form-item">
                    <label class="layui-form-label"><span style="color: red; ">*</span>用户状态：</label>
                    <div class="layui-input-block">
                        <input type="checkbox" id="status" name="status" lay-skin="switch" lay-text="启用|停用">
                    </div>
                </div>
            </div>
        </div>

        <div class="layui-row">
            <div class="layui-col-sm6 layui-col-md6">
                <div class="layui-form-item">
                    <label class="layui-form-label"><span style="color: red; ">*</span>岗位：</label>
                    <div class="layui-input-block">
                        <select name="post">
                            <option th:each="post:${posts}" th:value="${post.postId}" th:text="${post.postName}"
                                    th:disabled="${post.status == '1'}"></option>
                        </select>

                        <!--  <select id="post" class="form-control select2-multiple" multiple>
                              <option th:each="post:${posts}" th:value="${post.postId}" th:text="${post.postName}" th:disabled="${post.status == '1'}"></option>
                          </select>-->
                    </div>
                </div>
            </div>
            <div class="layui-col-sm6 layui-col-md6">
                <div class="layui-form-item">
                    <label class="layui-form-label"><span style="color: red; ">*</span>角色：</label>
                    <div class="layui-input-block">
                        <label th:each="role:${roles}">
                            <input type="checkbox" name="role" lay-skin="primary" th:value="${role.roleId}" th:title="${role.roleName}" th:disabled="${role.status == '1'}">
                        </label>
                    </div>
                </div>
            </div>
        </div>

        <fieldset class="layui-elem-field layui-field-title">
            <legend>其他信息</legend>
        </fieldset>
        <div class="layui-row">
            <div class="layui-col-sm12 layui-col-md12">
                <div class="layui-form-item layui-form-text">
                    <label class="layui-form-label">备注：</label>
                    <div class="layui-input-block">
                        <textarea name="remark" maxlength="500" class="layui-textarea" rows="3"></textarea>
                    </div>
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="formDemo">保 存</button>
                <button type="reset" class="layui-btn layui-btn-primary">关 闭</button>
            </div>
        </div>
    </form>
</div>

<!--    <button type="button" class="btn btn-sm btn-primary" onclick="submitHandler()"><i class="fa fa-check"></i>保 存</button>&nbsp;-->
<!--    <button type="button" class="btn btn-sm btn-danger" onclick="closeItem()"><i class="fa fa-reply-all"></i>关 闭 </button>-->
<th:block th:insert="include :: footer-js"/>
<script>
    var prefix = ctx + "system/user";
    var $,form;
    //全局定义一次, 加载formSelects
    layui.config({
        base: 'layui/lay/ext/formSelects/' //此处路径请自行处理, 可以使用绝对路径
    }).extend({
        formSelects: 'formSelects-v4'
    });
    //加载模块
    layui.use(['jquery', 'form', 'formSelects'], function () {
        $ = layui.jquery;
        form = layui.form;
        var formSelects = layui.formSelects;

    });


    /*$("#form-user-add").validate({
        onkeyup: false,
        rules:{
            loginName:{
                minlength: 2,
                maxlength: 20,
                remote: {
                    url: prefix + "/checkLoginNameUnique",
                    type: "post",
                    dataType: "json",
                    data: {
                        name : function() {
                            return $.common.trim($("#loginName").val());
                        }
                    },
                    dataFilter: function(data, type) {
                        return $.validate.unique(data);
                    }
                }
            },
            password:{
                minlength: 5,
                maxlength: 20
            },
            email:{
                email:true,
                remote: {
                    url: prefix + "/checkEmailUnique",
                    type: "post",
                    dataType: "json",
                    data: {
                        name: function () {
                            return $.common.trim($("#email").val());
                        }
                    },
                    dataFilter: function (data, type) {
                        return $.validate.unique(data);
                    }
                }
            },
            phonenumber:{
                isPhone:true,
                remote: {
                    url: prefix + "/checkPhoneUnique",
                    type: "post",
                    dataType: "json",
                    data: {
                        name: function () {
                            return $.common.trim($("#phonenumber").val());
                        }
                    },
                    dataFilter: function (data, type) {
                        return $.validate.unique(data);
                    }
                }
            },
        },
        messages: {
            "loginName": {
                remote: "用户已经存在"
            },
            "email": {
                remote: "Email已经存在"
            },
            "phonenumber":{
                remote: "手机号码已经存在"
            }
        },
        focusCleanup: true
    });*/

    function submitHandler() {
        if ($.validate.form()) {
            var data = $("#form-user-add").serializeArray();
            var status = $("input[id='status']").is(':checked') == true ? 0 : 1;
            var roleIds = $.form.selectCheckeds("role");
            var postIds = $.form.selectSelects("post");
            data.push({"name": "status", "value": status});
            data.push({"name": "roleIds", "value": roleIds});
            data.push({"name": "postIds", "value": postIds});
            $.operate.saveTab(prefix + "/add", data);
        }
    }

    /*用户管理-新增-选择部门树*/
    function selectDeptTree() {
        var treeId = $("#treeId").val();
        var deptId = $.common.isEmpty(treeId) ? "100" : $("#treeId").val();
        var url = ctx + "system/dept/selectDeptTree/" + deptId;
        var options = {
            title: '选择部门',
            width: "380",
            url: url,
            callBack: doSubmit
        };
        $.modal.openOptions(options);
    }

    function doSubmit(index, layero) {
        var tree = layero.find("iframe")[0].contentWindow.$._tree;
        if ($.tree.notAllowParents(tree)) {
            var body = layer.getChildFrame('body', index);
            $("#treeId").val(body.find('#treeId').val());
            $("#treeName").val(body.find('#treeName').val());
            layer.close(index);
        }
    }
</script>
</body>
</html>
